package sin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

@SuppressWarnings("serial")
public class Monitor extends JFrame{
	JLabel[] lblMonitor = new JLabel[2];
	JButton btnIni = new JButton("Iniciar");
    int elemento = 0;			
    boolean disponible;	
    boolean generando;	
    
    private Monitor() {
    	super("Monitor");
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }); 
        
        initLabel();
        pack();
        
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width/2 - getWidth()/2, 
        		    screenSize.height/2 - getHeight()/2);

        this.setResizable(false);
        setVisible(true);
    }
    
    private void initLabel() {
    	getContentPane().setLayout(new GridLayout(3,1,5,5));
        for(int i = 0; i < lblMonitor.length; i++) {
            lblMonitor[i] = new JLabel();
            lblMonitor[i].setFont(new java.awt.Font("Dialog", 0, 24));
            lblMonitor[i].setForeground(java.awt.Color.BLUE);
            lblMonitor[i].setHorizontalAlignment(SwingConstants.CENTER);
            lblMonitor[i].setBorder(new EtchedBorder(EtchedBorder.RAISED));
            lblMonitor[i].setPreferredSize(new java.awt.Dimension(200, 30)); 
            getContentPane().add(lblMonitor[i]);
        }
        
        btnIni.setFont(new java.awt.Font("Verdana", 0, 16));
        getContentPane().add(btnIni);
        btnIni.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		btnIni.setEnabled(false);
        		lblMonitor[1].setText("");
        	    disponible = false;	
        	    generando = true;	        		
                new Pone().start();
                new Quita().thread.start(); 
         	}
        });
    }
    
    private synchronized void pone(int n) {
    	while (disponible) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
    	
        elemento = n;
        lblMonitor[0].setText(String.valueOf(elemento));
        disponible = true;
        
        notify();
     }
    
    private synchronized void quita() {
        StringBuffer sbuf = new StringBuffer(lblMonitor[1].getText());
        while (!disponible) {
            try {
                wait();
            } catch (InterruptedException e) { }
        }
        
        disponible = false;
        sbuf.append(" ").append(String.valueOf(elemento));
        lblMonitor[1].setText(new String(sbuf));
        
        notify();
    }
    
    class Pone extends Thread {
         public void run() {
            for (int i = 0; i < 8; i++) {
                pone( (int) (Math.random()*10) );
            }
            generando = false;
        }
    }
    
    class Quita implements Runnable {
    	  Thread thread = new Thread(this);
          public void run() {
        	while (generando) {
                quita();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) { 
                    e.printStackTrace();	}            
        	}
        	quita();
            btnIni.setEnabled(true);
    		lblMonitor[0].setText("");
        }
    }    

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            	new Monitor();
            }
        });
     }
    
}
